package com.eqdd.yiqidian.ui.yiqiyue;

import android.databinding.DataBindingUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ProgressBar;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewRefreshCustom;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.bean.rebuild.HttpResultRows;
import com.eqdd.yiqidian.bean.rebuild.YueAttentionListBean;
import com.eqdd.yiqidian.bean.rebuild.YueAttentionListBean;
import com.eqdd.yiqidian.bean.rebuild.YueAttentionListBean;
import com.eqdd.yiqidian.box.ItemDecoration.SectionDividerItemDecoration;
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource;
import com.eqdd.yiqidian.helper.mvchelper.Rx2DataSource;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.JsonConverter;
import com.eqdd.yiqidian.utils.DensityUtil;
import com.eqdd.yiqidian.utils.HttpUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okrx2.adapter.FlowableBody;
import com.shizhefei.mvc.MVCCoolHelper;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;

/**
 * @author吕志豪 .
 * @date 17-12-13  下午1:05.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class YueFansListActivity extends BaseActivity {

    private RecyclerViewRefreshCustom dataBinding;
    private SlimAdapterEx<YueAttentionListBean> slimAdapterEx;
    private int pageNum;
    private MVCCoolHelper<List<YueAttentionListBean>> mvcHelper;

    @Override
    public void initBinding() {

        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview_refresh);
        initTopTitleBar(View.VISIBLE, "粉丝");
    }

    @Override
    public void initData() {
        dataBinding.recyclerView.addItemDecoration(new SectionDividerItemDecoration(this)
                .setHeadHeight(0)
                .setLeftPadding(DensityUtil.percentW(15))
                .setRightPadding(DensityUtil.percentW(15))
                .setLastBootomHeght(0)
        );
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_82, new SlimInjector<YueAttentionListBean>() {
            @Override
            public void onInject(YueAttentionListBean data, IViewInjector injector) {
                ProgressBar progressBar = (ProgressBar) injector.findViewById(R.id.progress);
                progressBar.setIndeterminate(true);
                injector.text(R.id.tv_left, data.getCreaterupname())
                        .image(R.id.iv_head, data.getCreateriphoto())
                        .visibility(R.id.progress, data.isLoading() ? View.VISIBLE : View.INVISIBLE)
                        .background(R.id.bt_attention, data.isAttention() ? getResources().getDrawable(R.drawable.shape_6) : getResources().getDrawable(R.drawable.shape_2))
                        .textColor(R.id.bt_attention, data.isAttention() ? getResources().getColor(R.color.hint_color) : getResources().getColor(R.color.white))
                        .text(R.id.bt_attention, data.isLoading() ? "" : (data.isAttention() ? "互相关注" : "+ 关注"))
                        .clicked(R.id.bt_attention, v -> {
                            if (!data.isLoading() && !data.isAttention()) {
                                data.setLoading(true);
                                slimAdapterEx.notifyItemChanged(slimAdapterEx.getData().indexOf(data));
                                attention(data.getUserGuid(), slimAdapterEx.getData().indexOf(data));
                            }
                        });
            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        ModelRx2DataSource<YueAttentionListBean> dataSource = new ModelRx2DataSource<>(new ModelRx2DataSource.OnLoadSource() {
            @Override
            public Flowable<List> loadSource(int page, Rx2DataSource.DoneActionRegister register) {
                pageNum = page == 1 ? 0 : pageNum;
                return OkGo.<HttpResultRows<YueAttentionListBean>>post(HttpConfig.BASE_URL + HttpConfig.YUE_FANS)
                        .params("page", pageNum)
                        .params("userGuid", AppConstent.user.getGuid())
                        .converter(new JsonConverter<HttpResultRows<YueAttentionListBean>>() {
                            @Override
                            public void test() {
                                super.test();
                            }
                        }).adapt(new FlowableBody<>())
                        .flatMap(listResultBean -> {
                            pageNum = listResultBean.getItems().getPage();
                            if (listResultBean.getStatus() == 200) {
                                return Flowable.just(listResultBean.getItems() == null ? new ArrayList<YueAttentionListBean>() : listResultBean.getItems().getRows());
                            } else {
                                ToastUtil.showShort(listResultBean.getMsg());
                                return Flowable.just(new ArrayList<>());
                            }
                        });

            }
        });
        slimAdapterEx.updateData(new ArrayList<>());
        mvcHelper = new MVCCoolHelper<>(dataBinding.coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapterEx);
        mvcHelper.refresh();
    }

    private void attention(String guid, int pos) {
        HttpUtil.attention(guid, () -> {
            slimAdapterEx.getDataItem(pos).setAttention(true);
            slimAdapterEx.getDataItem(pos).setLoading(false);
            slimAdapterEx.notifyItemChanged(pos);
        });
    }

    @Override
    public void setView() {

    }
}
